package com.example;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Shape;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import org.pict.gui.*;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class InpOutput extends JFrame{
    public InpOutput(){
        super("Input Output Example");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(500, 500);
        int i = 10;
        ComponentList components = new ComponentList();
        //Processes listProcesses = new Processes();
        //Channels listChannels = new Channels();
        getContentPane().add(components);
        //getContentPane().add(listChannels);
        getContentPane().setBackground(Color.BLACK);
        _Channel ch = new _Channel(i*10, i*10, (i+10)*10, (i+10)*10);
        components.addToList(ch);
	Output out = new Output(i*10, i*10, 10, 10);
	components.addProcess(i*10, i*10, 10, 10, out);
        i+=10;
        Input inp = new Input(i*10, i*10, 10, 10);
        components.addProcess(i*10, i*10, 10, 10, inp);
        out.addConnnection(ch, inp);
        out.parallel(inp, out);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }
    public static void main(String a[]){
        new InpOutput();
    }
}
class ComponentList extends JPanel{
    private List<_Channel> chanls = new ArrayList<_Channel>();
    private List<_Process> procs = new ArrayList<_Process>();
    public void addToList(_Channel chan){
        chanls.add(chan);
    }
    @Override
    protected void paintComponent(Graphics g){
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;
        for(_Channel ch: chanls){
            g2.drawLine(ch.x1, ch.y1, ch.x2, ch.y2);
            //g2.fill((Shape)ch);
        }
        for(_Process p: procs){
            g2.drawRect(p.x, p.y, p.width, p.height);
            g2.setColor(new Color(p.x, p.y, p.width));
            g2.fillRect(p.x, p.y, p.width, p.height);
        }
    }
    
    private static final int PREF_W = 500;
    private static final int PREF_H = PREF_W;
    public void addProcess(int x, int y, int width, int height, _Process proc){
        //Rectangle rect = new Rectangle(x, y, width, height);
        procs.add(proc);
    }
    @Override
    public Dimension getPreferredSize(){
        return new Dimension(PREF_W, PREF_H);
    }
}
class Processes extends JPanel{
    private List<_Process> procs = new ArrayList<_Process>();
    private static final int PREF_W = 500;
    private static final int PREF_H = PREF_W;
    public void addProcess(int x, int y, int width, int height, _Process proc){
        //Rectangle rect = new Rectangle(x, y, width, height);
        procs.add(proc);
    }
    @Override
    public Dimension getPreferredSize(){
        return new Dimension(PREF_W, PREF_H);
    }
    @Override
    protected void paintComponent(Graphics g){
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;
        for(_Process p: procs){
            g2.drawRect(p.x, p.y, p.width, p.height);
            g2.setColor(new Color(p.x, p.y, p.width));
            g2.fillRect(p.x, p.y, p.width, p.height);
        }
    }
}
class Output extends _Process{
        public Output(int x, int y, int w, int h){
            super(x, y, w, h);
        }
	@Override
	public void run(){
		System.out.println("entered output");
		for(int val = 0; val < 10; val++){
			for(int i = 0; i < connections.size(); i++){
				_Message msg = new _Message();
				msg.dict.put("Integer", val);
				msg.dict.put("String", "helloworld");
				_Channel ch = (_Channel) connections.keySet().toArray()[i];
				this.send(ch, msg);
                                System.out.println("Send :"+val);
			}
		}
	}
}
class Input extends _Process{
        public Input(int x, int y, int w, int h){
            super(x, y, w, h);
        }
	@Override
	public void run(){
		System.out.println("entered input");
		int j = 10;
		while(j-- !=0){
			for(int i = 0; i < connections.size(); i++){
				_Message msg = new _Message();
				this.receive((_Channel)connections.keySet().toArray()[i], msg);
				Enumeration<?> e = msg.dict.keys();
				while(e.hasMoreElements()){
					Comparable<?> key = (Comparable<?>)e.nextElement();
					Comparable<?> val = (Comparable<?>)msg.dict.get(key);
					System.out.println("Received :"+val);
				}
			}
		}
	}
}
